package org.gdstash.description;

import java.util.Comparator;

public class ItemComparator implements Comparator<BonusInfoSort> {
    @Override
    public int compare(final BonusInfoSort bis1, final BonusInfoSort bis2) {
        final boolean base1 = bis1.isBaseStat();
        final boolean base2 = bis2.isBaseStat();
        if (base1 && !base2) {
            return -1;
        }
        if (base2 && !base1) {
            return 1;
        }
        final int rate1 = this.rateInfoType(bis1);
        final int rate2 = this.rateInfoType(bis2);
        if (rate1 < rate2) {
            return 1;
        }
        if (rate1 > rate2) {
            return -1;
        }
        final int type1 = bis1.getTypeInt();
        final int type2 = bis2.getTypeInt();
        if (type1 < type2) {
            return 1;
        }
        if (type1 > type2) {
            return -1;
        }
        final int tag1 = bis1.getTagInt();
        final int tag2 = bis2.getTagInt();
        if (tag1 < tag2) {
            return 1;
        }
        if (tag1 > tag2) {
            return -1;
        }
        final int prio1 = bis1.prio;
        final int prio2 = bis2.prio;
        if (prio1 < prio2) {
            return 1;
        }
        if (prio1 > prio2) {
            return -1;
        }
        return 0;
    }

    protected int rateInfoType(final BonusInfoSort bis) {
        int prio = 0;
        if (bis.getTag().equals("DEF_BLOCK_CHANCE")) {
            return 700;
        }
        if (bis.getTag().equals("STAT_SHIELD_RECOVERY_TIME")) {
            return 650;
        }
        if (bis.getInfoType() == 2) {
            prio = 600;
            if (bis.isGlobal()) {
                if (bis.getGlobalCategory() == 1) {
                    prio -= 10;
                }
                if (bis.getGlobalCategory() == 3) {
                    prio -= 20;
                }
                if (bis.getGlobalCategory() == 4) {
                    prio -= 25;
                }
                if (bis.getGlobalCategory() == 5) {
                    prio -= 30;
                }
                if (bis.getGlobalCategory() == 6) {
                    prio -= 35;
                }
                if (bis.getGlobalCategory() == 7) {
                    prio -= 40;
                }
                if (bis.getGlobalCategory() == 2) {
                    prio -= 50;
                }
                if (bis.getGlobalCategory() == 8) {
                    prio -= 60;
                }
                if (bis.getGlobalCategory() == 0) {
                    prio -= 70;
                }
            }
            return prio;
        }
        if (bis.getInfoType() == 1) {
            return 500;
        }
        if (bis.getInfoType() == 4) {
            return 400;
        }
        if (bis.getInfoType() == 3) {
            prio = 300;
            if (bis.isGlobal()) {
                if (bis.getGlobalCategory() == 1) {
                    prio -= 10;
                }
                if (bis.getGlobalCategory() == 3) {
                    prio -= 20;
                }
                if (bis.getGlobalCategory() == 4) {
                    prio -= 25;
                }
                if (bis.getGlobalCategory() == 5) {
                    prio -= 30;
                }
                if (bis.getGlobalCategory() == 6) {
                    prio -= 35;
                }
                if (bis.getGlobalCategory() == 7) {
                    prio -= 40;
                }
                if (bis.getGlobalCategory() == 2) {
                    prio -= 50;
                }
                if (bis.getGlobalCategory() == 8) {
                    prio -= 60;
                }
                if (bis.getGlobalCategory() == 0) {
                    prio -= 70;
                }
            }
            return prio;
        }
        if (bis.getInfoType() == 5) {
            return 200;
        }
        if (bis.getInfoType() == 6) {
            return 100;
        }
        return 0;
    }
}
